﻿using Nop.Core.Caching;
using Nop.Core.Domain.Blogs;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Configuration;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Media;
using Nop.Core.Domain.News;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Polls;
using Nop.Core.Domain.Stores;
using Nop.Core.Domain.Topics;
using Nop.Core.Events;
using Nop.Core.Infrastructure;
using Nop.Services.Events;

namespace Nop.Web.Infrastructure.Cache
{
	/// <summary>
	/// Model cache event consumer (used for caching presentation layer models)
	/// </summary>
	public partial class ModelCacheEventConsumer: 
		//languages
		IConsumer<EntityInserted<Language>>,
		IConsumer<EntityUpdated<Language>>,
		IConsumer<EntityDeleted<Language>>,
		//currencies
		IConsumer<EntityInserted<Currency>>,
		IConsumer<EntityUpdated<Currency>>,
		IConsumer<EntityDeleted<Currency>>,
		//settings
		IConsumer<EntityInserted<Setting>>,
		IConsumer<EntityUpdated<Setting>>,
		//manufacturers
		IConsumer<EntityInserted<Manufacturer>>,
		IConsumer<EntityUpdated<Manufacturer>>,
		IConsumer<EntityDeleted<Manufacturer>>,
		//product manufacturers
		IConsumer<EntityInserted<ProductManufacturer>>,
		IConsumer<EntityUpdated<ProductManufacturer>>,
		IConsumer<EntityDeleted<ProductManufacturer>>,
		//categories
		IConsumer<EntityInserted<Category>>,
		IConsumer<EntityUpdated<Category>>,
		IConsumer<EntityDeleted<Category>>,
		//product categories
		IConsumer<EntityInserted<ProductCategory>>,
		IConsumer<EntityUpdated<ProductCategory>>,
		IConsumer<EntityDeleted<ProductCategory>>,
		//products
		IConsumer<EntityInserted<Product>>,
		IConsumer<EntityUpdated<Product>>,
		IConsumer<EntityDeleted<Product>>,
		//product variants
		IConsumer<EntityInserted<ProductVariant>>,
		IConsumer<EntityUpdated<ProductVariant>>,
		IConsumer<EntityDeleted<ProductVariant>>,
		//product tags
		IConsumer<EntityInserted<ProductTag>>,
		IConsumer<EntityUpdated<ProductTag>>,
		IConsumer<EntityDeleted<ProductTag>>,
		//specification attributes
		IConsumer<EntityInserted<SpecificationAttribute>>,
		IConsumer<EntityUpdated<SpecificationAttribute>>,
		IConsumer<EntityDeleted<SpecificationAttribute>>,
		//specification attribute options
		IConsumer<EntityInserted<SpecificationAttributeOption>>,
		IConsumer<EntityUpdated<SpecificationAttributeOption>>,
		IConsumer<EntityDeleted<SpecificationAttributeOption>>,
		//Product specification attribute
		IConsumer<EntityInserted<ProductSpecificationAttribute>>,
		IConsumer<EntityUpdated<ProductSpecificationAttribute>>,
		IConsumer<EntityDeleted<ProductSpecificationAttribute>>,
		//Topics
		IConsumer<EntityInserted<Topic>>,
		IConsumer<EntityUpdated<Topic>>,
		IConsumer<EntityDeleted<Topic>>,
		//Category templates
		IConsumer<EntityInserted<CategoryTemplate>>,
		IConsumer<EntityUpdated<CategoryTemplate>>,
		IConsumer<EntityDeleted<CategoryTemplate>>,
		//Manufacturer templates
		IConsumer<EntityInserted<ManufacturerTemplate>>,
		IConsumer<EntityUpdated<ManufacturerTemplate>>,
		IConsumer<EntityDeleted<ManufacturerTemplate>>,
		//Product templates
		IConsumer<EntityInserted<ProductTemplate>>,
		IConsumer<EntityUpdated<ProductTemplate>>,
		IConsumer<EntityDeleted<ProductTemplate>>,
		//Orders
		IConsumer<EntityInserted<Order>>,
		IConsumer<EntityUpdated<Order>>,
		IConsumer<EntityDeleted<Order>>,
		//Order product variants
		IConsumer<EntityInserted<OrderProductVariant>>,
		IConsumer<EntityUpdated<OrderProductVariant>>,
		IConsumer<EntityDeleted<OrderProductVariant>>,
		//Pictures
		IConsumer<EntityInserted<Picture>>,
		IConsumer<EntityUpdated<Picture>>,
		IConsumer<EntityDeleted<Picture>>,
		//Product picture mapping
		IConsumer<EntityInserted<ProductPicture>>,
		IConsumer<EntityUpdated<ProductPicture>>,
		IConsumer<EntityDeleted<ProductPicture>>,
		//polls
		IConsumer<EntityInserted<Poll>>,
		IConsumer<EntityUpdated<Poll>>,
		IConsumer<EntityDeleted<Poll>>,
		//blog posts
		IConsumer<EntityInserted<BlogPost>>,
		IConsumer<EntityUpdated<BlogPost>>,
		IConsumer<EntityDeleted<BlogPost>>,
		//news items
		IConsumer<EntityInserted<NewsItem>>,
		IConsumer<EntityUpdated<NewsItem>>,
		IConsumer<EntityDeleted<NewsItem>>,
		//states/province
		IConsumer<EntityInserted<StateProvince>>,
		IConsumer<EntityUpdated<StateProvince>>,
		IConsumer<EntityDeleted<StateProvince>>,
		//customer roles
		IConsumer<EntityUpdated<CustomerRole>>,
		IConsumer<EntityDeleted<CustomerRole>>,
		//picture links
		IConsumer<EntityUpdated<PictureLink>>,
		IConsumer<EntityDeleted<PictureLink>>,
		//store pictures
		IConsumer<EntityUpdated<Store>>,
		IConsumer<EntityDeleted<Store>>
	{
		/// <summary>
		/// Key for ManufacturerNavigationModel caching
		/// </summary>
		/// <remarks>
		/// {0} : current manufacturer id
		/// {1} : language id
		/// </remarks>
		public const string MANUFACTURER_NAVIGATION_MODEL_KEY = "nop.pres.manufacturer.navigation-{0}-{1}";
		public const string MANUFACTURER_NAVIGATION_PATTERN_KEY = "nop.pres.manufacturer.navigation";
		
		/// <summary>
		/// Key for CategoryNavigationModel caching
		/// </summary>
		/// <remarks>
		/// {0} : current category id
		/// {1} : current product id
		/// {2} : language id
		/// {3} : store-id
		/// {4} : comma separated list of customer roles
		/// </remarks>
		public const string CATEGORY_NAVIGATION_MODEL_KEY = "nop.pres.category.navigation-{0}-{1}-{2}-{3}-{4}";
		public const string CATEGORY_NAVIGATION_PATTERN_KEY = "nop.pres.category.navigation";

		/// <summary>
		/// Key for GetChildCategoryIds method results caching
		/// </summary>
		/// <remarks>
		/// {0} : parent category id
		/// {1} : show hidden?
		/// {2} : comma separated list of customer roles
		/// </remarks>
		public const string CATEGORY_CHILD_IDENTIFIERS_MODEL_KEY = "nop.pres.category.childidentifiers-{0}-{1}-{2}";
		public const string CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY = "nop.pres.category.childidentifiers";
		
		/// <summary>
		/// Key for ProductBreadcrumbModel caching
		/// </summary>
		/// <remarks>
		/// {0} : product id
		/// {1} : language id
		/// {2} : comma separated list of customer roles
		/// </remarks>
		public const string PRODUCT_BREADCRUMB_MODEL_KEY = "nop.pres.product.breadcrumb-{0}-{1}-{2}";
		public const string PRODUCT_BREADCRUMB_PATTERN_KEY = "nop.pres.product.breadcrumb";


		/// <summary>
		/// Key for ProductTagModel caching
		/// </summary>
		/// <remarks>
		/// {0} : product id
		/// {1} : language id
		/// </remarks>
		public const string PRODUCTTAG_BY_PRODUCT_MODEL_KEY = "nop.pres.producttag.byproduct-{0}-{1}";
		public const string PRODUCTTAG_BY_PRODUCT_PATTERN_KEY = "nop.pres.producttag.byproduct";

		/// <summary>
		/// Key for PopularProductTagsModel caching
		/// </summary>
		/// <remarks>
		/// {0} : language id
		/// </remarks>
		public const string PRODUCTTAG_POPULAR_MODEL_KEY = "nop.pres.producttag.popular-{0}";
		public const string PRODUCTTAG_POPULAR_PATTERN_KEY = "nop.pres.producttag.popular";

		/// <summary>
		/// Key for ProductManufacturers model caching
		/// </summary>
		/// <remarks>
		/// {0} : product id
		/// {1} : language id
		/// </remarks>
		public const string PRODUCT_MANUFACTURERS_MODEL_KEY = "nop.pres.product.manufacturers-{0}-{1}";
		public const string PRODUCT_MANUFACTURERS_PATTERN_KEY = "nop.pres.product.manufacturers";

		/// <summary>
		/// Key for ProductSpecificationModel caching
		/// </summary>
		/// <remarks>
		/// {0} : product id
		/// {1} : language id
		/// </remarks>
		public const string PRODUCT_SPECS_MODEL_KEY = "nop.pres.product.specs-{0}-{1}";
		public const string PRODUCT_SPECS_PATTERN_KEY = "nop.pres.product.specs";

		/// <summary>
		/// Key for TopicModel caching
		/// </summary>
		/// <remarks>
		/// {0} : topic id
		/// {1} : language id
		/// </remarks>
		public const string TOPIC_MODEL_KEY = "nop.pres.topic.details-{0}-{1}";
		public const string TOPIC_PATTERN_KEY = "nop.pres.topic.details";

		/// <summary>
		/// Key for CategoryTemplate caching
		/// </summary>
		/// <remarks>
		/// {0} : category template id
		/// </remarks>
		public const string CATEGORY_TEMPLATE_MODEL_KEY = "nop.pres.categorytemplate-{0}";
		public const string CATEGORY_TEMPLATE_PATTERN_KEY = "nop.pres.categorytemplate";

		/// <summary>
		/// Key for ManufacturerTemplate caching
		/// </summary>
		/// <remarks>
		/// {0} : manufacturer template id
		/// </remarks>
		public const string MANUFACTURER_TEMPLATE_MODEL_KEY = "nop.pres.manufacturertemplate-{0}";
		public const string MANUFACTURER_TEMPLATE_PATTERN_KEY = "nop.pres.manufacturertemplate";

		/// <summary>
		/// Key for ProductTemplate caching
		/// </summary>
		/// <remarks>
		/// {0} : product template id
		/// </remarks>
		public const string PRODUCT_TEMPLATE_MODEL_KEY = "nop.pres.producttemplate-{0}";
		public const string PRODUCT_TEMPLATE_PATTERN_KEY = "nop.pres.producttemplate";

		/// <summary>
		/// Key for bestsellers identifiers displayed on the home page
		/// </summary>
		public const string HOMEPAGE_BESTSELLERS_IDS_KEY = "nop.pres.bestsellers.homepage-{0}-{1}-{2}";  // storeId, categoryId, roleIds
		public const string HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY = "nop.pres.bestsellers.homepage";

		/// <summary>
		/// Key for weekly new product identifiers displayed on the home page
		/// </summary>
		/// <remarks>
		/// {0} : store id
		/// {1} : category id
		/// </remarks>
		public const string HOMEPAGE_WEEKLYNEWPRODUCTS_IDS_KEY = "nop.pres.weeklynewproducts.homepage-{0}-{1}-{2}";  
		public const string HOMEPAGE_WEEKLYNEWPRODUCTS_IDS_PATTERN_KEY = "nop.pres.weeklynewproducts.homepage";

        public const string HOMEPAGE_FEATUREDPRODUCTS_IDS_KEY = "nop.pres.featuredproducts.homepage-{0}-{1}-{2}-{3}"; //regionId, pagenumber, pictureSize, roleIds
        public const string HOMEPAGE_FEATUREDPRODUCTS_IDS_PATTERN_KEY = "nop.pres.featuredproducts.homepage";
        /// <summary>
        /// Key for products identifiers displayed on the generic / all products page
        /// </summary>
        public const string PRODUCT_ALL_IDS_KEY = "nop.pres.product.all-{0}-{1}-{2}";  // regionId, storeId, categoryId
		public const string PRODUCT_ALL_IDS_PATTERN_KEY = "nop.pres.product.all";

		/// <summary>
		/// Key for "also purchased" product identifiers displayed on the product details page
		/// </summary>
		/// <remarks>
		/// {0} : current product id
		/// </remarks>
		public const string PRODUCTS_ALSO_PURCHASED_IDS_KEY = "nop.pres.alsopuchased-{0}";
		public const string PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY = "nop.pres.alsopuchased";
        
        /// <summary>
        /// Key for default product picture caching
        /// </summary>
        /// <remarks>
        /// {0} : product id
        /// {1} : picture size
        /// {2} : value indicating whether a default picture is displayed in case if no real picture exists
        /// {3} : language ID ("alt" and "title" can depend on localized product name)
        /// {4} : is connection SSL secured?
        /// </remarks>
        public const string PRODUCT_DEFAULTPICTURE_MODEL_KEY = "nop.pres.product.picture-{0}-{1}-{2}-{3}-{4}";
		public const string PRODUCT_DEFAULTPICTURE_PATTERN_KEY = "nop.pres.product.picture";

		/// <summary>
		/// Key for default store picture caching
		/// </summary>
		/// <remarks>
		/// {0} : store id
		/// {1} : picture size
		/// {2} : value indicating whether a default picture is displayed in case if no real picture exists
		/// {3} : language ID ("alt" and "title" can depend on localized product name)
		/// {4} : is connection SSL secured?
		/// </remarks>
		public const string STORE_PICTURE_MODEL_KEY = "nop.pres.store.picture-{0}-{1}-{2}-{3}-{4}";
		public const string STORE_PICTURE_PATTERN_KEY = "nop.pres.store.picture";

		/// <summary>
		/// Key for picture link caching
		/// </summary>
		/// <remarks>
		/// {0} : picture link id
		/// {1} : language ID ("alt" and "title" can depend on localized category name)
		/// {2} : is connection SSL secured?
		/// </remarks>
		public const string PICTURE_LINK_MODEL_KEY = "nop.pres.media.picturelink-{0}-{1}-{2}";
		public const string PICTURE_LINK_PATTERN_KEY = "nop.pres.media.picturelink";

		/// <summary>
		/// Key for category picture caching
		/// </summary>
		/// <remarks>
		/// {0} : category id
		/// {1} : picture size
		/// {2} : value indicating whether a default picture is displayed in case if no real picture exists
		/// {3} : language ID ("alt" and "title" can depend on localized category name)
		/// {4} : is connection SSL secured?
		/// </remarks>
		public const string CATEGORY_PICTURE_MODEL_KEY = "nop.pres.category.picture-{0}-{1}-{2}-{3}-{4}";
		public const string CATEGORY_PICTURE_PATTERN_KEY = "nop.pres.category.picture";

		/// <summary>
		/// Key for manufacturer picture caching
		/// </summary>
		/// <remarks>
		/// {0} : manufacturer id
		/// {1} : picture size
		/// {2} : value indicating whether a default picture is displayed in case if no real picture exists
		/// {3} : language ID ("alt" and "title" can depend on localized manufacturer name)
		/// {4} : is connection SSL secured?
		/// </remarks>
		public const string MANUFACTURER_PICTURE_MODEL_KEY = "nop.pres.manufacturer.picture-{0}-{1}-{2}-{3}-{4}";
		public const string MANUFACTURER_PICTURE_PATTERN_KEY = "nop.pres.manufacturer.picture";

		/// <summary>
		/// Key for cart picture caching
		/// </summary>
		/// <remarks>
		/// {0} : product variant id
		/// {1} : picture size
		/// {2} : value indicating whether a default picture is displayed in case if no real picture exists
		/// {3} : language ID ("alt" and "title" can depend on localized product name)
		/// {4} : is connection SSL secured?
		/// </remarks>
		public const string CART_PICTURE_MODEL_KEY = "nop.pres.cart.picture-{0}-{1}-{2}-{3}-{4}";
		public const string CART_PICTURE_PATTERN_KEY = "nop.pres.cart.picture";

		/// <summary>
		/// Key for home page polls
		/// </summary>
		/// <remarks>
		/// {0} : language ID
		/// </remarks>
		public const string HOMEPAGE_POLLS_MODEL_KEY = "nop.pres.poll.homepage-{0}";
		/// <summary>
		/// Key for polls by system name
		/// </summary>
		/// <remarks>
		/// {0} : poll system name
		/// {1} : language ID
		/// </remarks>
		public const string POLL_BY_SYSTEMNAME__MODEL_KEY = "nop.pres.poll.systemname-{0}-{1}";
		public const string POLLS_PATTERN_KEY = "nop.pres.poll.";

		/// <summary>
		/// Key for blog tag list model
		/// </summary>
		/// <remarks>
		/// {0} : language ID
		/// </remarks>
		public const string BLOG_TAGS_MODEL_KEY = "nop.pres.blog.tags-{0}";
		/// <summary>
		/// Key for blog archive (years, months) block model
		/// </summary>
		/// <remarks>
		/// {0} : language ID
		/// </remarks>
		public const string BLOG_MONTHS_MODEL_KEY = "nop.pres.blog.months-{0}";
		public const string BLOG_PATTERN_KEY = "nop.pres.blog.";

		/// <summary>
		/// Key for home page news
		/// </summary>
		/// <remarks>
		/// {0} : language ID
		/// </remarks>
		public const string HOMEPAGE_NEWSMODEL_KEY = "nop.pres.news.homepage-{0}";
		public const string NEWS_PATTERN_KEY = "nop.pres.news.";
		
		/// <summary>
		/// Key for states by country id
		/// </summary>
		/// <remarks>
		/// {0} : country ID
		/// {0} : addEmptyStateIfRequired value
		/// {0} : language ID
		/// </remarks>
		public const string STATEPROVINCES_BY_COUNTRY_MODEL_KEY = "nop.pres.stateprovinces.bycountry-{0}-{1}-{2}";
		public const string STATEPROVINCES_PATTERN_KEY = "nop.pres.stateprovinces.";

		/// <summary>
		/// Key for available languages
		/// </summary>
		public const string AVAILABLE_LANGUAGES_MODEL_KEY = "nop.pres.languages.all";
		public const string AVAILABLE_LANGUAGES_PATTERN_KEY = "nop.pres.languages.";

		/// <summary>
		/// Key for available regions
		/// </summary>
		public const string AVAILABLE_REGIONS_MODEL_KEY = "nop.pres.regions.all";
		public const string AVAILABLE_REGIONS_PATTERN_KEY = "nop.pres.regions.";

		/// <summary>
		/// Key for available currencies
		/// </summary>
		/// <remarks>
		/// {0} : language ID
		/// </remarks>
		public const string AVAILABLE_CURRENCIES_MODEL_KEY = "nop.pres.currencies.all-{0}";
		public const string AVAILABLE_CURRENCIES_PATTERN_KEY = "nop.pres.currencies.";

		private readonly ICacheManager _cacheManager;
		
		public ModelCacheEventConsumer()
		{
			//TODO inject static cache manager using constructor
			this._cacheManager = EngineContext.Current.ContainerManager.Resolve<ICacheManager>("nop_cache_static");
		}

		//languages
		public void HandleEvent(EntityInserted<Language> eventMessage)
		{
			//clear all localizable models
			_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
			_cacheManager.RemoveByPattern(AVAILABLE_LANGUAGES_PATTERN_KEY);
			_cacheManager.RemoveByPattern(AVAILABLE_CURRENCIES_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Language> eventMessage)
		{
			//clear all localizable models
			_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
			_cacheManager.RemoveByPattern(AVAILABLE_LANGUAGES_PATTERN_KEY);
			_cacheManager.RemoveByPattern(AVAILABLE_CURRENCIES_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Language> eventMessage)
		{
			//clear all localizable models
			_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
			_cacheManager.RemoveByPattern(AVAILABLE_LANGUAGES_PATTERN_KEY);
			_cacheManager.RemoveByPattern(AVAILABLE_CURRENCIES_PATTERN_KEY);
		}
		
		//currencies
		public void HandleEvent(EntityInserted<Currency> eventMessage)
		{
			_cacheManager.RemoveByPattern(AVAILABLE_CURRENCIES_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Currency> eventMessage)
		{
			_cacheManager.RemoveByPattern(AVAILABLE_CURRENCIES_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Currency> eventMessage)
		{
			_cacheManager.RemoveByPattern(AVAILABLE_CURRENCIES_PATTERN_KEY);
		}

		//settings
		public void HandleEvent(EntityInserted<Setting> eventMessage)
		{
			//clear models which depend on settings
		}
		public void HandleEvent(EntityUpdated<Setting> eventMessage)
		{
			//clear models which depend on settings
			_cacheManager.RemoveByPattern(PRODUCTTAG_POPULAR_PATTERN_KEY); //depends on CatalogSettings.NumberOfProductTags
			_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY); //depends on CatalogSettings.ManufacturersBlockItemsToDisplay
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY); //depends on CatalogSettings.ShowCategoryProductNumber and CatalogSettings.ShowCategoryProductNumberIncludingSubcategories
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY); //depends on CatalogSettings.NumberOfBestsellersOnHomepage
			_cacheManager.RemoveByPattern(HOMEPAGE_WEEKLYNEWPRODUCTS_IDS_PATTERN_KEY); // depends on CatalogSettings.RecentlyAddedProductsNumber
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY); //depends on CatalogSettings.ProductsAlsoPurchasedNumber
			_cacheManager.RemoveByPattern(BLOG_PATTERN_KEY); //depends on BlogSettings.NumberOfTags
			_cacheManager.RemoveByPattern(NEWS_PATTERN_KEY); //depends on NewsSettings.MainPageNewsCount
			
		}
		//manufacturers
		public void HandleEvent(EntityInserted<Manufacturer> eventMessage)
		{
			_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Manufacturer> eventMessage)
		{
			_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(MANUFACTURER_PICTURE_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Manufacturer> eventMessage)
		{
			_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(MANUFACTURER_PICTURE_PATTERN_KEY);
		}

		//product manufacturers
		public void HandleEvent(EntityInserted<ProductManufacturer> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<ProductManufacturer> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<ProductManufacturer> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
		}
		
		//categories
		public void HandleEvent(EntityInserted<Category> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Category> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Category> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
		}

		//product categories
		public void HandleEvent(EntityInserted<ProductCategory> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<ProductCategory> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<ProductCategory> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
		 }

		//products
		public void HandleEvent(EntityInserted<Product> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(HOMEPAGE_WEEKLYNEWPRODUCTS_IDS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Product> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CART_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(HOMEPAGE_WEEKLYNEWPRODUCTS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Product> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(HOMEPAGE_WEEKLYNEWPRODUCTS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}

		//product variants
		public void HandleEvent(EntityInserted<ProductVariant> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<ProductVariant> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CART_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<ProductVariant> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}

		//product tags
		public void HandleEvent(EntityInserted<ProductTag> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCTTAG_POPULAR_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTTAG_BY_PRODUCT_MODEL_KEY);
		}
		public void HandleEvent(EntityUpdated<ProductTag> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCTTAG_POPULAR_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTTAG_BY_PRODUCT_MODEL_KEY);
		}
		public void HandleEvent(EntityDeleted<ProductTag> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCTTAG_POPULAR_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTTAG_BY_PRODUCT_MODEL_KEY);
		}
		
		//specification attributes
		public void HandleEvent(EntityInserted<SpecificationAttribute> eventMessage)
		{
		}
		public void HandleEvent(EntityUpdated<SpecificationAttribute> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<SpecificationAttribute> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
		}
		
		//specification attribute options
		public void HandleEvent(EntityInserted<SpecificationAttributeOption> eventMessage)
		{
		}
		public void HandleEvent(EntityUpdated<SpecificationAttributeOption> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<SpecificationAttributeOption> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
		}
		
		//Product specification attribute
		public void HandleEvent(EntityInserted<ProductSpecificationAttribute> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<ProductSpecificationAttribute> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<ProductSpecificationAttribute> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_SPECS_PATTERN_KEY);
		}

		//Topics
		public void HandleEvent(EntityInserted<Topic> eventMessage)
		{
		}
		public void HandleEvent(EntityUpdated<Topic> eventMessage)
		{
			_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Topic> eventMessage)
		{
			_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
		}
		
		//Category templates
		public void HandleEvent(EntityInserted<CategoryTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_TEMPLATE_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<CategoryTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_TEMPLATE_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<CategoryTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(CATEGORY_TEMPLATE_PATTERN_KEY);
		}

		//Manufacturer templates
		public void HandleEvent(EntityInserted<ManufacturerTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(MANUFACTURER_TEMPLATE_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<ManufacturerTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(MANUFACTURER_TEMPLATE_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<ManufacturerTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(MANUFACTURER_TEMPLATE_PATTERN_KEY);
		}

		//Product templates
		public void HandleEvent(EntityInserted<ProductTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_TEMPLATE_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<ProductTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_TEMPLATE_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<ProductTemplate> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_TEMPLATE_PATTERN_KEY);
		}


		//Orders
		public void HandleEvent(EntityInserted<Order> eventMessage)
		{
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Order> eventMessage)
		{
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Order> eventMessage)
		{
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}
		//Order product variants
		public void HandleEvent(EntityInserted<OrderProductVariant> eventMessage)
		{
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<OrderProductVariant> eventMessage)
		{
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<OrderProductVariant> eventMessage)
		{
			_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY);
			_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY);
		}

		//Pictures
		public void HandleEvent(EntityInserted<Picture> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(MANUFACTURER_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CART_PICTURE_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Picture> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(MANUFACTURER_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CART_PICTURE_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Picture> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(MANUFACTURER_PICTURE_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CART_PICTURE_PATTERN_KEY);
		}

		//Product picture mappings
		public void HandleEvent(EntityInserted<ProductPicture> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<ProductPicture> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<ProductPicture> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_DEFAULTPICTURE_PATTERN_KEY);
		}

		//Polls
		public void HandleEvent(EntityInserted<Poll> eventMessage)
		{
			_cacheManager.RemoveByPattern(POLLS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<Poll> eventMessage)
		{
			_cacheManager.RemoveByPattern(POLLS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Poll> eventMessage)
		{
			_cacheManager.RemoveByPattern(POLLS_PATTERN_KEY);
		}

		//Blog posts
		public void HandleEvent(EntityInserted<BlogPost> eventMessage)
		{
			_cacheManager.RemoveByPattern(BLOG_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<BlogPost> eventMessage)
		{
			_cacheManager.RemoveByPattern(BLOG_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<BlogPost> eventMessage)
		{
			_cacheManager.RemoveByPattern(BLOG_PATTERN_KEY);
		}

		//News items
		public void HandleEvent(EntityInserted<NewsItem> eventMessage)
		{
			_cacheManager.RemoveByPattern(NEWS_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<NewsItem> eventMessage)
		{
			_cacheManager.RemoveByPattern(NEWS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<NewsItem> eventMessage)
		{
			_cacheManager.RemoveByPattern(NEWS_PATTERN_KEY);
		}

		//State/province
		public void HandleEvent(EntityInserted<StateProvince> eventMessage)
		{
			_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
		}
		public void HandleEvent(EntityUpdated<StateProvince> eventMessage)
		{
			_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<StateProvince> eventMessage)
		{
			_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
		}

		//customer roles
		public void HandleEvent(EntityUpdated<CustomerRole> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<CustomerRole> eventMessage)
		{
			_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
			_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
		}

		//picture links
		public void HandleEvent(EntityUpdated<PictureLink> eventMessage)
		{
			_cacheManager.RemoveByPattern(PICTURE_LINK_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<PictureLink> eventMessage)
		{
			_cacheManager.RemoveByPattern(PICTURE_LINK_PATTERN_KEY);
		}

		//stores
		public void HandleEvent(EntityUpdated<Store> eventMessage)
		{
			_cacheManager.RemoveByPattern(STORE_PICTURE_PATTERN_KEY);
		}
		public void HandleEvent(EntityDeleted<Store> eventMessage)
		{
			_cacheManager.RemoveByPattern(STORE_PICTURE_PATTERN_KEY);
		}
	}
}
